//
//  EXRChooseTypeViewController.m
//  HuiKaoClient
//
//  Created by 龚 杰 on 13-2-25.
//  Copyright (c) 2013年 龚 杰. All rights reserved.
//

#import "EXRChooseTypeViewController.h"
#import "MainTabBarController.h"

#import "AppDelegate.h"

#define  Province_id 1000
@interface EXRChooseTypeViewController ()

@end

@implementation EXRChooseTypeViewController

@synthesize SelectEXRid = m_SelectEXRid;
//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        // Custom initialization
//    }
//    return self;
//}
- (id)init{
    if(self = [super init]){
        m_EXRCData = [[NSMutableArray alloc] init];
        m_SelectEXRid = [[NSMutableArray alloc] init];
    }
    return self;
}
- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (m_SaveBttBarItem != nil) {
        [self.navigationController.navigationBar addSubview:m_SaveBttBarItem];
    }
    if (m_BackBttBarItem != nil) {
        [self.navigationController.navigationBar addSubview:m_BackBttBarItem];
    }
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (m_SaveBttBarItem.superview) {
        [m_SaveBttBarItem removeFromSuperview];
    }
    if (m_BackBttBarItem.superview) {
        [m_BackBttBarItem removeFromSuperview];
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
//    if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
//        if ([self.navigationController.navigationBar viewWithTag:0x300] == nil) {
//            UIImageView* tImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titleBar.png"]];
//            tImgView.tag =0x300;
//            tImgView.frame = CGRectMake(0, 0, 320, 44);
//            UILabel* title = [MainTabBarController setTitle:@"考试提醒"];
//            [self.navigationController.navigationBar addSubview:tImgView];
//            [self.navigationController.navigationBar addSubview:title];
//            [tImgView release];
//        }
//    }
    m_SaveBttBarItem = [UIButton buttonWithType:UIButtonTypeCustom];
    m_SaveBttBarItem.frame = CGRectMake(self.navigationController.navigationBar.frame.size.width-57, 7, 50, 30);
    [m_SaveBttBarItem setBackgroundImage:[UIImage imageNamed:@"button_right.png"] forState:UIControlStateNormal];
    [m_SaveBttBarItem setTitle:@"保存" forState:UIControlStateNormal];
    [m_SaveBttBarItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    m_SaveBttBarItem.titleLabel.font = [UIFont systemFontOfSize:13.0f];
    [m_SaveBttBarItem addTarget:self action:@selector(ChangeSave) forControlEvents:UIControlEventTouchUpInside];
    [m_SaveBttBarItem retain];
    
    m_BackBttBarItem = [UIButton buttonWithType:UIButtonTypeCustom];
    m_BackBttBarItem.frame = CGRectMake(7, 7, 50, 30);
    [m_BackBttBarItem setBackgroundImage:[UIImage imageNamed:@"button_back.png"] forState:UIControlStateNormal];
    [m_BackBttBarItem setTitle:@"返回" forState:UIControlStateNormal];
    [m_BackBttBarItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    m_BackBttBarItem.titleLabel.font = [UIFont systemFontOfSize:13.0f];
    [m_BackBttBarItem addTarget:self action:@selector(ChangeBack) forControlEvents:UIControlEventTouchUpInside];
    [m_BackBttBarItem retain];
    
    m_TableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height-44) style:UITableViewStyleGrouped];
    [m_TableView setBackgroundColor:BACKGROUND_COLOR];
    m_TableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    m_TableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [m_TableView setDataSource:self];
    [m_TableView setDelegate:self];
    [self.view addSubview:m_TableView];
    
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    if ([tAppDel.m_CityDataArr count]>0) {
        for (NSObject* object in tAppDel.m_CityDataArr) {
            [m_EXRCData addObject:object];
        }
    }else{
        HttpInterfaceManager* iHttpInterface = [HttpInterfaceManager sharedManager];
        iHttpInterface.delegate = self;
        [iHttpInterface ProvincesListRequestGet];
        [self addHudLoad];
    }

}


- (void)ChangeSave
{
    //if ([m_SelectEXRid count] > 0) {
        NSMutableString* strary = [[NSMutableString alloc] init];
        for (NSString* txt in m_SelectEXRid) {
            NSString* text = [NSString stringWithFormat:@"%@,",txt];
            [strary appendString:text];
        }
        NSUserDefaults* tUserDef = [NSUserDefaults standardUserDefaults];
        NSString* userid = [tUserDef objectForKey:@"user_id"];
        HttpInterfaceManager* iHttpInterface = [HttpInterfaceManager sharedManager];
        iHttpInterface.delegate = self;
        [iHttpInterface AddExReRequestGet:userid province_ids:strary];
        [self addHudLoad];
    //}else{
    //    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    //    [tAppDel showWithCustomView:@"提示" detailText:@"请选择地区" isCue:YES delayTime:2.0f];
    //}
}
- (void)ChangeBack
{
    self.hidesBottomBarWhenPushed = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [m_EXRCData count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* tCellstr = @"EXRCell";
    EXRChooseTpyeCell* tCell = [tableView dequeueReusableCellWithIdentifier:tCellstr];
    if (tCell == nil) {
        tCell = [[[EXRChooseTpyeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tCellstr] autorelease];
        tCell.accessoryType = UITableViewCellAccessoryNone;
        tCell.selectionStyle = UITableViewCellSelectionStyleNone;
        tCell.delegate = self;
    }
    NSUInteger row = indexPath.row;
    NSDictionary* dic = [m_EXRCData objectAtIndex:row];
    NSString* name = (NSString*)[dic objectForKey:@"name"];
    NSInteger ID = ((NSString*)[dic objectForKey:@"id"]).integerValue;
    [tCell initData:name tag:ID  select:m_SelectEXRid];


    return tCell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView* tSectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
    [tSectionHeaderView setBackgroundColor:[UIColor clearColor]];
    UILabel* tLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 30)];
    [tLab setBackgroundColor:[UIColor clearColor]];
    [tLab setFont:[UIFont boldSystemFontOfSize:16]];
    [tLab setTextColor:[UIColor blackColor]];
    [tLab setText:@"设置要提醒的考试"];
    [tSectionHeaderView addSubview:tLab];
    [tLab release];
    return [tSectionHeaderView autorelease];
}
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}
#pragma mark MBProgressHUDDelegate
- (void)hudWasHidden:(MBProgressHUD *)hud{
    m_MBHUD.delegate = nil;
	[m_MBHUD removeFromSuperview];
	[m_MBHUD release];
	m_MBHUD=nil;
}

-(void) addHudLoad{
	if (m_MBHUD == nil) {
		m_MBHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
		// Add HUD to screen
        AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
		[tAppDel.window addSubview:m_MBHUD];
		// Regisete for HUD callbacks so we can remove it from the window at the right time
		m_MBHUD.delegate = self;
		m_MBHUD.labelText = @"请求数据中...";
		[m_MBHUD show:YES];
	}
}
#pragma mark HttpInterfaceManageDelegate
-(void)RequestResultsProvincesList:(NSDictionary *)dic
{
    [self hudWasHidden:nil];
    if (dic != nil) {
        NSDictionary* tListDic = [dic objectForKey:@"list"];
        NSString* tNumStr = nil;
        for (int i= 1000; i<1032; i++) {
            tNumStr = [NSString stringWithFormat:@"%d",i];
            NSDictionary* tCityDic = [tListDic objectForKey:tNumStr];
            if (tCityDic != nil) {
                [m_EXRCData addObject:tCityDic];
            }else{
                break;
            }
        }
        [m_TableView reloadData];
    }
}
-(void)RequestResultsAddExRe:(NSDictionary*)dic
{
    [self hudWasHidden:nil];
    NSInteger result = ((NSString*)[dic objectForKey:@"result"]).integerValue;
    if (result == 1) {
        AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
        [tAppDel showWithCustomView:@"提示" detailText:@"保存成功" isCue:YES delayTime:2.0f];
        [self performSelector:@selector(popview) withObject:nil afterDelay:2.0f];
    }else{
        AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
        [tAppDel showWithCustomView:@"提示" detailText:@"保存失败" isCue:YES delayTime:2.0f];
    }
}

-(void)RequestResultsErrer:(NSDictionary*)dic
{
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [tAppDel showWithCustomView:@"提示" detailText:[dic objectForKey:@"errorMsg"] isCue:YES delayTime:2.0f];
    [self hudWasHidden:nil];
}
#pragma mark EXRChooseTpyeCellDelegate
- (void)getTag:(NSDictionary*)dic
{
    NSString* isChoose = [dic objectForKey:@"isChoose"];
    NSString* ID = (NSString*)[dic objectForKey:@"id"];
    NSLog(@"%@",ID);
    if ([isChoose isEqualToString:@"YES"]) {
        [m_SelectEXRid addObject:ID];
         NSLog(@"jia = %d",[m_SelectEXRid count]);
    }else{
        [m_SelectEXRid removeObject:ID];
        NSLog(@"san = %d",[m_SelectEXRid count]);
    }
}
- (void)popview
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [m_SaveBttBarItem release];
    [m_SelectEXRid release];
    [m_TableView release];
    m_Button1 = nil;
    m_Button2 = nil;
    [m_BackBttBarItem release];
    [m_EXRCData release];
    m_SelectEXRid = nil;
    [super dealloc];
}
@end
